//
//  PlayLevelRecorder.cpp
//  FruitCrush
//
//  Created by gaoyu on 15/8/4.
//
//

#include "PlayLevelRecorder.h"
#include "LevelScreenConfig.h"

static PlayLevelRecorder *s_SharedPlayLevelRecorder = nullptr;

PlayLevelRecorder* PlayLevelRecorder::getInstance()
{
    if (!s_SharedPlayLevelRecorder)
    {
        s_SharedPlayLevelRecorder = new (std::nothrow) PlayLevelRecorder();
        CCASSERT(s_SharedPlayLevelRecorder, "FATAL: Not enough memory");
        s_SharedPlayLevelRecorder->init();
    }
    
    return s_SharedPlayLevelRecorder;
}

PlayLevelRecorder::PlayLevelRecorder()
:nowPlayLevel(0)
{
}

bool PlayLevelRecorder::init(void)
{
    return true;
}

void PlayLevelRecorder::writeLevelPassInfo(int level, int pass_status)
{
	__String *str = __String::create("");
	str->initWithFormat("%s%d", "pass_status_of_level", level);
	UserDefault::getInstance()->setIntegerForKey(str->getCString(), pass_status);
}

int PlayLevelRecorder::readLevelPassInfo(int level)
{
	__String *str = __String::create("");
	str->initWithFormat("%s%d", "pass_status_of_level", level);
	return UserDefault::getInstance()->getIntegerForKey(str->getCString(), 0);
}

void PlayLevelRecorder::writeLevelStarNumInfo(int level, int starNum)
{
	__String *str = __String::create("");
	str->initWithFormat("%s%d", "star_number_of_level", level);
	UserDefault::getInstance()->setIntegerForKey(str->getCString(), starNum);
}

int PlayLevelRecorder::readLevelStarNumInfo(int level)
{
	__String *str = __String::create("");
	str->initWithFormat("%s%d", "star_number_of_level", level);
	return UserDefault::getInstance()->getIntegerForKey(str->getCString(), 0);
}

int PlayLevelRecorder::getLevelPassNum()
{
	int var = LevelScreenTypeType01;
	for (; var < LevelScreenTypeTypeMax; ++var) {
		if(readLevelPassInfo(var) == 0)
		{
			break;
		}
	}
	return var - 1;
}

int PlayLevelRecorder::getThreeStarLevelNum()
{
	int three_star_level_num = 0;
	for (int var = LevelScreenTypeType01; var < getLevelPassNum() + 1; ++var) {
		if(readLevelStarNumInfo(var) == 3)
		{
			three_star_level_num++;
		}
	}
	return three_star_level_num;
}
